<template>
  <div class="faq-container">
    <!-- 页面头部 -->
    <div class="page-header">
      <div class="header-bg"></div>
      <div class="header-content">
        <h1 class="page-title">常见问题</h1>
        <p class="page-subtitle">快速查找您关心的问题解答</p>
      </div>
    </div>

    <!-- 主内容区 -->
    <div class="main-content">
      <div class="container">
        <div class="faq-layout">
          <!-- 左侧分类导航 -->
          <div class="category-sidebar">
            <h3 class="sidebar-title">问题分类</h3>
            <div class="category-list">
              <div
                v-for="category in categories"
                :key="category.id"
                :class="['category-item', { active: activeCategory === category.id }]"
                @click="switchCategory(category.id)"
              >
                <i :class="'el-icon-' + category.icon"></i>
                <span class="category-name">{{ category.name }}</span>
              </div>
            </div>

            <!-- 搜索框 -->
            <div class="search-section">
              <h3 class="sidebar-title">搜索问题</h3>
              <el-input
                v-model="searchQuery"
                placeholder="输入关键词搜索"
                prefix-icon="el-icon-search"
                class="search-input"
                @input="handleSearch"
              ></el-input>
              <div v-if="searchQuery" class="search-tips">
                <span class="tips-text">共找到 {{ filteredFaqs.length }} 个相关问题</span>
                <el-button type="text" size="small" @click="clearSearch" class="clear-btn">清空</el-button>
              </div>
            </div>

            <!-- 联系我们提示 -->
            <div class="contact-prompt">
              <h3 class="sidebar-title">没找到答案？</h3>
              <p class="prompt-text">如果您的问题没有在这里得到解答，请联系我们的客服团队。</p>
              <router-link to="/contact" class="contact-button">
                <i class="el-icon-message"></i>
                联系客服
              </router-link>
  </div>
          </div>
        </div>

        <!-- 右侧问题列表 -->
          <div class="faq-content">
            <div v-if="searchQuery && filteredFaqs.length === 0" class="no-results">
              <el-empty description="没有找到相关问题，请尝试其他关键词"></el-empty>
            </div>
            <div v-else>
              <div class="category-header">
                <h2 class="content-title">
                  {{ getCategoryName(activeCategory) }}
                </h2>
                <p class="content-desc">
                  {{ getCategoryDescription(activeCategory) }}
                </p>
              </div>

              <el-collapse
                v-model="activeNames"
                class="faq-collapse"
                @change="handleFaqChange"
              >
                <el-collapse-item
                  v-for="faq in displayFaqs"
                  :key="faq.id"
                  :name="faq.id.toString()"
                  class="faq-item"
                >
                  <template #title>
                    <div class="faq-title-wrapper">
                      <span class="faq-number">{{ faq.id }}</span>
                      <span class="faq-title">{{ faq.question }}</span>
                    </div>
                  </template>
                  <div class="faq-answer">
                    <div v-html="formatAnswer(faq.answer)"></div>
                    <div v-if="faq.relatedQuestions && faq.relatedQuestions.length > 0" class="related-questions">
                      <p class="related-title">相关问题：</p>
                      <ul class="related-list">
                        <li v-for="(related, index) in faq.relatedQuestions" :key="index">
                          <a href="#" @click.stop="openRelatedFaq(related.id)">
                            <el-icon class="related-icon"><i-ep-arrow-right /></el-icon>
                            {{ related.title }}
                          </a>
                        </li>
                      </ul>
                    </div>
                  </div>
                </el-collapse-item>
              </el-collapse>

              <!-- 分页控件 -->
              <div v-if="!searchQuery && paginatedFaqs.length > 0" class="pagination">
                <el-pagination
                  v-model:current-page="currentPage"
                  v-model:page-size="pageSize"
                  :page-sizes="[10, 20, 30]"
                  layout="total, sizes, prev, pager, next, jumper"
                  :total="currentCategoryFaqs.length"
                  @size-change="handleSizeChange"
                  @current-change="handleCurrentChange"
                ></el-pagination>
              </div>
            </div>
          </div>

        <!-- 常见问题推荐 -->
        <div v-if="!searchQuery" class="popular-section">
          <h2 class="popular-title">热门问题推荐</h2>
          <div class="popular-list">
            <div v-for="popular in popularFaqs" :key="popular.id" class="popular-item">
              <div class="popular-rank">
                <span v-if="popular.id <= 3" class="rank-number top">{{ popular.id }}</span>
                <span v-else class="rank-number">{{ popular.id }}</span>
              </div>
              <div class="popular-content">
                <a href="#" @click.stop="openRelatedFaq(popular.id)" class="popular-question">
                  {{ popular.question }}
                </a>
                <div class="popular-meta">
                  <span class="view-count">
                    <i class="el-icon-view"></i>
                    {{ popular.views }}次浏览
                  </span>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, computed, onMounted, watch } from 'vue'
import { ElEmpty, ElInput, ElButton, ElPagination, ElCollapse, ElCollapseItem, ElIcon } from 'element-plus'

// 分类数据
const categories = ref([
  { id: 'account', name: '账号问题', icon: 'user' },
  { id: 'product', name: '商品发布', icon: 'goods' },
  { id: 'transaction', name: '交易流程', icon: 'wallet' },
  { id: 'payment', name: '支付相关', icon: 'money' },
  { id: 'delivery', name: '物流配送', icon: 'truck' },
  { id: 'afterSale', name: '售后服务', icon: 'help-filled' }
])

// 当前激活的分类
const activeCategory = ref('account')

// 搜索关键词
const searchQuery = ref('')

// 手风琴激活的名称
const activeNames = ref([])

// 分页相关
const currentPage = ref(1)
const pageSize = ref(10)

// 模拟FAQ数据
const faqs = ref([
  // 账号相关
  {
    id: 1,
    category: 'account',
    question: '如何注册账号？',
    answer: '<p>注册账号非常简单，只需按照以下步骤操作：</p><ol><li>点击页面右上角的"注册"按钮</li><li>输入手机号码和验证码</li><li>设置密码</li><li>阅读并同意用户协议</li><li>点击"完成注册"</li></ol><p>注册成功后，您可以立即开始使用平台功能。</p>',
    views: 5200,
    relatedQuestions: [
      { id: 2, title: '如何修改密码？' },
      { id: 3, title: '忘记密码怎么办？' }
    ]
  },
  {
    id: 2,
    category: 'account',
    question: '如何修改密码？',
    answer: '<p>修改密码的步骤如下：</p><ol><li>登录账号后，进入"个人中心"</li><li>点击"账号安全"设置</li><li>选择"修改密码"</li><li>输入原密码和新密码</li><li>点击"确认修改"</li></ol><p>建议您定期更换密码，确保账号安全。</p>',
    views: 3800,
    relatedQuestions: [
      { id: 1, title: '如何注册账号？' },
      { id: 3, title: '忘记密码怎么办？' }
    ]
  },
  // 商品相关
  {
    id: 11,
    category: 'product',
    question: '如何发布商品？',
    answer: '<p>发布商品的流程如下：</p><ol><li>登录账号后，点击页面顶部的"发布商品"按钮</li><li>填写商品基本信息（名称、分类、价格等）</li><li>上传商品图片（建议3-5张）</li><li>填写详细描述</li><li>选择交易方式和配送方式</li><li>点击"发布"按钮</li></ol><p>发布成功后，您的商品将在平台上展示。</p>',
    views: 4500,
    relatedQuestions: [
      { id: 12, title: '如何修改已发布的商品？' },
      { id: 13, title: '商品发布后多久可以显示？' }
    ]
  },
  // 交易相关
  {
    id: 21,
    category: 'transaction',
    question: '如何购买商品？',
    answer: '<p>购买商品的流程如下：</p><ol><li>浏览或搜索找到您感兴趣的商品</li><li>点击商品进入详情页，查看商品信息</li><li>如果对商品有疑问，可以点击"联系卖家"进行咨询</li><li>确认购买后，点击"立即购买"或"加入购物车"</li><li>选择交易方式和收货信息</li><li>完成支付</li><li>等待卖家发货或约定见面交易时间</li><li>收到商品并确认无误后，点击"确认收货"</li></ol><p>建议选择平台担保交易，确保交易安全。</p>',
    views: 2890,
    relatedQuestions: [
      { id: 22, title: '如何确保交易安全？' },
      { id: 23, title: '交易纠纷如何处理？' }
    ]
  },
  {
    id: 22,
    category: 'transaction',
    question: '如何确保交易安全？',
    answer: '<p>为确保交易安全，建议您采取以下措施：</p><ul><li>优先选择平台担保交易，避免直接转账</li><li>当面交易时选择人多、安全的公共场所</li><li>交易前仔细查看商品详情，如有疑问及时咨询卖家</li><li>收到商品后当面检查，确认无误再确认收货</li><li>保留交易过程中的聊天记录和支付凭证</li><li>遇到可疑情况及时联系平台客服</li></ul><p>平台也会通过实名认证、交易监控等方式保障交易安全。</p>',
    views: 3210,
    relatedQuestions: [
      { id: 21, title: '如何购买商品？' },
      { id: 23, title: '交易纠纷如何处理？' }
    ]
  }
])

// 计算属性：当前分类的问题
const currentCategoryFaqs = computed(() => {
  return faqs.value.filter(faq => faq.category === activeCategory.value)
})

// 计算属性：搜索过滤后的问题
const filteredFaqs = computed(() => {
  if (!searchQuery.value) return []
  
  const query = searchQuery.value.toLowerCase()
  return faqs.value.filter(faq => 
    faq.question.toLowerCase().includes(query) || 
    faq.answer.toLowerCase().includes(query)
  )
})

// 计算属性：分页后的问题
const paginatedFaqs = computed(() => {
  const start = (currentPage.value - 1) * pageSize.value
  const end = start + pageSize.value
  return currentCategoryFaqs.value.slice(start, end)
})

// 计算属性：要显示的问题
const displayFaqs = computed(() => {
  if (searchQuery.value) {
    return filteredFaqs.value
  }
  return paginatedFaqs.value
})

// 计算属性：热门问题（按浏览量排序）
const popularFaqs = computed(() => {
  return [...faqs.value]
    .sort((a, b) => b.views - a.views)
    .slice(0, 5)
})

// 方法：切换分类
const switchCategory = (categoryId) => {
  activeCategory.value = categoryId
  activeNames.value = []
  currentPage.value = 1
}

// 方法：处理搜索
const handleSearch = () => {
  activeNames.value = []
}

// 方法：清空搜索
const clearSearch = () => {
  searchQuery.value = ''
  activeNames.value = []
}

// 方法：处理手风琴变化
const handleFaqChange = (names) => {
  activeNames.value = names
}

// 方法：打开相关问题
const openRelatedFaq = (faqId) => {
  const faq = faqs.value.find(f => f.id === faqId)
  if (faq) {
    activeCategory.value = faq.category
    activeNames.value = [faqId.toString()]
    currentPage.value = 1
    // 滚动到问题位置
    setTimeout(() => {
      const element = document.getElementById(`faq-${faqId}`)
      if (element) {
        element.scrollIntoView({ behavior: 'smooth' })
      }
    }, 100)
  }
}

// 方法：处理分页大小变化
const handleSizeChange = (size) => {
  pageSize.value = size
  currentPage.value = 1
}

// 方法：处理当前页码变化
const handleCurrentChange = (current) => {
  currentPage.value = current
  activeNames.value = []
}

// 方法：获取分类名称
const getCategoryName = (categoryId) => {
  const category = categories.value.find(c => c.id === categoryId)
  return category ? category.name : '全部问题'
}

// 方法：获取分类描述
const getCategoryDescription = (categoryId) => {
  const descriptions = {
    account: '账号注册、登录、安全等相关问题',
    product: '商品发布、管理、修改等相关问题',
    transaction: '购买流程、交易安全、订单管理等问题',
    payment: '支付方式、退款、账单查询等相关问题',
    delivery: '配送方式、物流查询、收货管理等问题',
    afterSale: '退换货政策、售后保障、纠纷处理等问题'
  }
  return descriptions[categoryId] || '查看相关问题的详细解答'
}

// 方法：格式化答案内容
const formatAnswer = (html) => {
  // 可以在这里对HTML内容进行额外的处理
  return html
}

// 页面加载时初始化
onMounted(() => {
  // 初始化操作
})

// 监听搜索变化
watch(searchQuery, () => {
  activeNames.value = []
})
</script>

<style scoped>
/* 容器样式 */
.faq-container {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background: #f5f7fa;
}

/* 页面头部样式 */
.page-header {
  height: 300px;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
}

.header-bg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: url('../assets/images/header-bg.png') no-repeat center center;
  background-size: cover;
  opacity: 0.1;
}

.header-content {
  text-align: center;
  position: relative;
  z-index: 1;
}

.page-title {
  font-size: 36px;
  font-weight: 700;
  margin-bottom: 15px;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.2);
}

.page-subtitle {
  font-size: 16px;
  opacity: 0.9;
}

/* 主内容区样式 */
.main-content {
  flex: 1;
  padding: 60px 0;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.faq-layout {
  display: flex;
  gap: 40px;
}

/* 左侧分类导航样式 */
.category-sidebar {
  width: 300px;
  flex-shrink: 0;
}

.sidebar-title {
  font-size: 20px;
  font-weight: 700;
  color: #333;
  margin-bottom: 20px;
  position: relative;
  padding-left: 15px;
}

.sidebar-title::before {
  content: '';
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 4px;
  height: 20px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 2px;
}

.category-list {
  background: white;
  border-radius: 12px;
  padding: 15px 0;
  margin-bottom: 30px;
  box-shadow: 0 5px 20px rgba(0, 0, 0, 0.05);
}

.category-item {
  display: flex;
  align-items: center;
  gap: 15px;
  padding: 12px 25px;
  cursor: pointer;
  transition: all 0.3s ease;
  color: #666;
}

.category-item:hover {
  background: rgba(102, 126, 234, 0.05);
  color: #667eea;
}

.category-item.active {
  background: linear-gradient(135deg, rgba(102, 126, 234, 0.1) 0%, rgba(118, 75, 162, 0.1) 100%);
  color: #667eea;
  font-weight: 600;
}

.category-icon {
  font-size: 20px;
  width: 24px;
  text-align: center;
}

/* 搜索区域样式 */
.search-section {
  background: white;
  border-radius: 12px;
  padding: 25px;
  margin-bottom: 30px;
  box-shadow: 0 5px 20px rgba(0, 0, 0, 0.05);
}

.search-input {
  margin-bottom: 10px;
}

.search-tips {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-top: 10px;
  border-top: 1px solid #f0f0f0;
}

.tips-text {
  font-size: 14px;
  color: #999;
}

.clear-btn {
  font-size: 14px;
  color: #667eea;
}

/* 联系我们提示样式 */
.contact-prompt {
  background: linear-gradient(135deg, rgba(102, 126, 234, 0.1) 0%, rgba(118, 75, 162, 0.1) 100%);
  border-radius: 12px;
  padding: 25px;
  text-align: center;
}

.prompt-text {
  color: #666;
  margin-bottom: 20px;
  line-height: 1.6;
}

.contact-button {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 10px 20px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  text-decoration: none;
  border-radius: 25px;
  font-weight: 600;
  transition: all 0.3s ease;
}

.contact-button:hover {
  transform: translateY(-3px);
  box-shadow: 0 8px 25px rgba(102, 126, 234, 0.4);
}

/* 右侧问题列表样式 */
.faq-content {
  flex: 1;
  min-width: 0;
}

.category-header {
  margin-bottom: 30px;
}

.content-title {
  font-size: 28px;
  font-weight: 700;
  color: #333;
  margin-bottom: 15px;
  position: relative;
  display: inline-block;
}

.content-title::after {
  content: '';
  position: absolute;
  bottom: -8px;
  left: 0;
  width: 60px;
  height: 4px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 2px;
}

.content-desc {
  color: #666;
  font-size: 16px;
  line-height: 1.6;
}

/* 手风琴样式 */
.faq-collapse {
  background: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 5px 20px rgba(0, 0, 0, 0.05);
}

.faq-item {
  border-bottom: 1px solid #f0f0f0;
}

.faq-item:last-child {
  border-bottom: none;
}

:deep(.el-collapse-item__header) {
  padding: 20px 30px;
  background: white;
  color: #333;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
}

:deep(.el-collapse-item__header:hover) {
  background: #f8f9fa;
}

:deep(.el-collapse-item__content) {
  padding: 0 30px 25px;
}

.faq-title-wrapper {
  display: flex;
  align-items: center;
  gap: 15px;
}

.faq-number {
  width: 30px;
  height: 30px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  font-weight: 600;
  color: white;
  flex-shrink: 0;
}

.faq-title {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.faq-answer {
  color: #666;
  line-height: 1.8;
  padding-top: 20px;
}

.faq-answer p {
  margin-bottom: 15px;
}

.faq-answer p:last-child {
  margin-bottom: 0;
}

.faq-answer ol,
.faq-answer ul {
  margin-bottom: 15px;
  padding-left: 30px;
}

.faq-answer ol li,
.faq-answer ul li {
  margin-bottom: 10px;
}

.faq-answer ol li:last-child,
.faq-answer ul li:last-child {
  margin-bottom: 0;
}

/* 相关问题样式 */
.related-questions {
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px solid #f0f0f0;
}

.related-title {
  font-weight: 600;
  color: #333;
  margin-bottom: 15px;
}

.related-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.related-list li {
  margin-bottom: 10px;
}

.related-list li:last-child {
  margin-bottom: 0;
}

.related-list a {
  display: flex;
  align-items: center;
  gap: 10px;
  color: #667eea;
  text-decoration: none;
  transition: all 0.3s ease;
  padding: 5px 0;
}

.related-list a:hover {
  color: #764ba2;
  transform: translateX(5px);
}

.related-icon {
  font-size: 14px;
  color: #999;
}

/* 分页样式 */
.pagination {
  display: flex;
  justify-content: center;
  margin-top: 40px;
}

/* 没有结果样式 */
.no-results {
  padding: 80px 0;
  text-align: center;
}

/* 热门问题样式 */
.popular-section {
  background: #f8f9fa;
  border-radius: 12px;
  padding: 40px;
  margin-top: 60px;
}

.popular-title {
  font-size: 24px;
  font-weight: 700;
  color: #333;
  margin-bottom: 30px;
  text-align: center;
  position: relative;
  padding-bottom: 15px;
}

.popular-title::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 60px;
  height: 4px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 2px;
}

.popular-list {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 20px;
}

.popular-item {
  display: flex;
  gap: 20px;
  background: white;
  padding: 20px;
  border-radius: 10px;
  transition: all 0.3s ease;
  box-shadow: 0 5px 20px rgba(0, 0, 0, 0.05);
}

.popular-item:hover {
  transform: translateY(-5px);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
}

.popular-rank {
  flex-shrink: 0;
}

.rank-number {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  background: #f0f0f0;
  border-radius: 50%;
  font-weight: 700;
  color: #666;
  font-size: 16px;
}

.rank-number.top {
  background: linear-gradient(135deg, #ff9a9e 0%, #fad0c4 100%);
  color: white;
}

.popular-content {
  flex: 1;
}

.popular-question {
  color: #333;
  font-weight: 600;
  text-decoration: none;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  margin-bottom: 10px;
  transition: color 0.3s ease;
}

.popular-question:hover {
  color: #667eea;
}

.popular-meta {
  font-size: 14px;
  color: #999;
}

.view-icon {
  margin-right: 5px;
  font-size: 14px;
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .faq-layout {
    flex-direction: column;
  }
  
  .category-sidebar {
    width: 100%;
  }
  
  .category-list {
    display: flex;
    overflow-x: auto;
    padding: 15px;
    gap: 10px;
    scrollbar-width: none;
    -ms-overflow-style: none;
  }
  
  .category-list::-webkit-scrollbar {
    display: none;
  }
  
  .category-item {
    white-space: nowrap;
    padding: 10px 20px;
    border-radius: 25px;
  }
}

@media (max-width: 768px) {
  .page-title {
    font-size: 30px;
  }
  
  .content-title {
    font-size: 24px;
  }
  
  .popular-list {
    grid-template-columns: 1fr;
  }
  
  .popular-section {
    padding: 30px 20px;
  }
}

@media (max-width: 480px) {
  .page-header {
    height: 200px;
  }
  
  .page-title {
    font-size: 24px;
  }
  
  :deep(.el-collapse-item__header) {
    padding: 15px 20px;
  }
  
  .faq-title-wrapper {
    flex-direction: column;
    align-items: flex-start;
    gap: 10px;
  }
  
  .faq-number {
    align-self: flex-start;
  }
  
  .popular-item {
    flex-direction: column;
    gap: 15px;
  }
  
  .popular-rank {
    align-self: flex-start;
  }
}
</style>
